package com.drakulo.hi.logic.actions;

import com.dk.hi.core.SwitchManager;
import com.dk.hi.core.exceptions.HIException;

/**
 * <b>Action on switches</b>
 * <p>
 * This action modifies the state of a switch
 * </p>
 */
public class SwitchAction implements Action {
	/**
	 * Switch global id
	 */
	private Integer switchId;

	/**
	 * Switch state wanted. 3 possibilities :
	 * <ol>
	 * <li>Null : switch state will be toggled</li>
	 * <li>True : switch will be turned on</li>
	 * <li>False : switch will be turned off</li>
	 * </ol>
	 */
	private Boolean state;

	/**
	 * Constructor
	 * 
	 * @param switchId
	 *            - Switch global id
	 * @param state
	 *            - Switch state wanted
	 */
	public SwitchAction(Integer switchId, Boolean state) {
		this.switchId = switchId;
		this.state = state;
	}

	@Override
	public void run() throws HIException {
		Boolean s = this.state;
		if (s == null) {
			s = !SwitchManager.getStateOf(this.switchId);
		}
		if (s) {
			SwitchManager.enable(this.switchId);
		} else {
			SwitchManager.disable(this.switchId);
		}
	}

}
